﻿using BW.Rabbit.Infrasttucture.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.Rabbit.Infrasttucture.Implement
{
    public class BaseRepository<T>:IBaseRepository<T> where T:class
    {
        private RabbidDBContext db;

        public BaseRepository(RabbidDBContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> AddAsync(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> AddRangeAsync(T entity)
        {
            await db.Set<T>().AddRangeAsync(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns>返回任务</returns>
        public  IQueryable<T> GetAll()
        {
            return  db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 更新数据?
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> UpdateAsync(T entity)
        {
             db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量更新数据?
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> UpdateRangeAsync(T entity)
        {
             db.Set<T>().UpdateRange(entity);
            return await db.SaveChangesAsync();
        }
    }
}
